mw2c$ = jQuery.noConflict();
var ret = true;
mw2c$(document).ready(function () {
    mw2c$('div.field').append('<div class="msg"></div>');
    mw2c$('#mw2cMessages, div.msg').hide();
    mw2c$('form#mw2c').submit(function () {
        ret = true;
        mw2c$('#mw2cFields').find('input, textarea, select').each(function () {
            mw2c$(this).trigger('blur');
        });
        if (ret == false) {
            mw2c$('#mw2cMessages').show();
        }
        return ret;
    });
    
    
    
    
    
});

/* MAIN VALIDATION FUNCTIONS */

function validateRequired(elem, msg) {
    if (mw2c$(elem).val() == '') {
        markError(elem, msg);
    } else {
        unmarkError(elem);
    }
}

var emailFilter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

function validateEmail(elem, msg) {
    if (mw2c$(elem).val() != '' && !emailFilter.test(mw2c$(elem).val())) {
        markError(elem, msg);
    } else if (mw2c$(elem).val() != '' && emailFilter.test(mw2c$(elem).val())) {
        unmarkError(elem);
    }
}

function validateNumber(elem, msg, format, number) {
    var numberFilter = "";
    if (format == '&,') {
        if (number != '0') {
            numberFilter = /(^-?[0-9]+('[0-9]{3})*(\,[0-9]+)?$)|(^-?[0-9]+('[0-9]{3})*$)|(^-?\,[0-9]+$)/;
        } else {
            numberFilter = /(^-?[0-9]+('[0-9]{3})*$)/;
        }
    }
    if (format == '&.') {
        if (number != '0') {
            numberFilter = /(^-?[0-9]+('[0-9]{3})*(\.[0-9]+)?$)|(^-?[0-9]+('[0-9]{3})*$)|(^-?\.[0-9]+$)/;
        } else {
            numberFilter = /(^-?[0-9]+('[0-9]{3})*$)/;
        }
    }
    if (format == " ,") {
        if (number != '0') {
            numberFilter = /(^-?[0-9]+( [0-9]{3})*(\,[0-9]+)?$)|(^-?[0-9]+( [0-9]{3})*$)|(^-?\,[0-9]+$)/;
        } else {
            numberFilter = /(^-?[0-9]+( [0-9]{3})*$)/;
        }
    }
    if (format == ",.") {
        if (number != '0') {
            numberFilter = /(^-?[0-9]+(\,[0-9]{3})*(\.[0-9]+)?$)|(^-?[0-9]+(\,[0-9]{3})*$)|(^-?\.[0-9]+$)/;
        } else {
            numberFilter = /(^-?[0-9]+(\,[0-9]{3})*$)/;
        }
    }
    if (format == ".,") {
        if (number != '0') {
            numberFilter = /(^-?[0-9]+(\.[0-9]{3})*(\,[0-9]+)?$)|(^-?[0-9]+(\.[0-9]{3})*$)|(^-?\,[0-9]+$)/;
        } else {
            numberFilter = /(^-?[0-9]+(\.[0-9]{3})*$)/;
        }
    }
    var valNumberLength = mw2c$(elem).val().length - 1;
    var numberDecimals = 0;
    if (mw2c$(elem).val().indexOf('.') != -1 || mw2c$(elem).val().indexOf(',') != -1) {
        if (mw2c$(elem).val().indexOf('.') != -1 && mw2c$(elem).val().indexOf('.') > mw2c$(elem).val().indexOf(',')) {
            numberDecimals = valNumberLength - mw2c$(elem).val().indexOf('.');
        }
        if (mw2c$(elem).val().indexOf(',') != -1 && mw2c$(elem).val().indexOf(',') > mw2c$(elem).val().indexOf('.')) {
            numberDecimals = valNumberLength - mw2c$(elem).val().indexOf(',');
        }
    }
    if (mw2c$(elem).val() != '' && !numberFilter.test(mw2c$(elem).val())) {
        markError(elem, msg);
    } else if (mw2c$(elem).val() != '' && numberFilter.test(mw2c$(elem).val())) {
        if (numberDecimals != 0) {
            if (numberDecimals != number) {
                markError(elem, msg);
            } else {
                unmarkError(elem);
            }
        } else {
            unmarkError(elem);
        }
    }
}

var alphaNumFilter = /[^a-zA-Z0-9 ]/g;

function validateAlphaNum(elem, msg) {
    if (mw2c$(elem).val() != '' && mw2c$(elem).val().search(alphaNumFilter) != -1) {
        markError(elem, msg);
    } else if (mw2c$(elem).val() != '' && mw2c$(elem).val().search(alphaNumFilter) == -1) {
        unmarkError(elem);
    }
}

function validateDate(elem, msg, format) {
    if (mw2c$(elem).val() != '' && isDate(mw2c$(elem).val(), format) == false) {
        markError(elem, msg);
    } else if (mw2c$(elem).val() != '' && isDate(mw2c$(elem).val(), format) == true) {
        unmarkError(elem);
    }
}

function validateDateTime(elem, msg, format) {
    if (mw2c$(elem).val() != '' && isValidDateTime(mw2c$(elem).val(), format) == false) {
        markError(elem, msg);
    } else if (mw2c$(elem).val() != '' && isValidDateTime(mw2c$(elem).val(), format) == true) {
        unmarkError(elem);
    }
}

var urlFilter = /(([a-zA-Z0-9-])+)\.([a-zA-Z0-9]{2,4})+$/;

function validateURL(elem, msg) {
    if (mw2c$(elem).val() != '' && !urlFilter.test(mw2c$(elem).val())) {
        markError(elem, msg);
    } else if (mw2c$(elem).val() != '' && urlFilter.test(mw2c$(elem).val())) {
        unmarkError(elem);
    }
} /* MAIN VALIDATION FUNCTIONS */

function markError(elem, msg) {
    if (!mw2c$(elem).parent('div.field').hasClass('invalid')) {
        mw2c$(elem).parent('div.field').addClass('invalid');
        mw2c$(elem).next('div.msg').html(msg).show();
    }
    ret = false;
}

function unmarkError(elem) {
    mw2c$(elem).next('div.msg').html('').hide();
    mw2c$(elem).parent('div.field').removeClass('invalid');
}

function auxDateTime(value, format, formatForDate) {
    var indexTime = 0;
    var indexAMPM = 0;
    var time = "";
    var type = "";
    var date = "";
    var dateFormat = "";
    if (format == "HH:mm dd/MM/yyyy") {
        indexTime = value.indexOf(" ");
        hour = value.substring(0, indexTime);
        date = value.substring(indexTime + 1);
    } else {
        if (format == "yyyy. M. d a h:mm") {
            if (value.indexOf(" AM ") == -1 || value.indexOf(" PM ") == -1) {
                return false;
            }
            if (value.indexOf(" AM ") != -1) {
                indexTime = value.indexOf(" AM ");
            }
            if (value.indexOf(" PM ") != -1) {
                indexTime = value.indexOf(" PM ");
            }
            date = value.substring(0, indexTime);
            hour = time.substring(indexTime + 4);
        } else {
            if (format == "yyyy-M-d ah:mm") {
                indexTime = value.indexOf(" ");
                date = value.substring(0, indexTime);
                type = value.substring(indexTime + 1, 2);
                hour = time.substring(indexTime + 3);
                if (type != "AM" && type != "PM") {
                    return false;
                }
            } else {
                indexTime = value.indexOf(" ");
                date = value.substring(0, indexTime);
                if (format.indexOf("a") != -1) {
                    time = value.substring(indexTime + 1);
                    indexAMPM = time.indexOf(" ");
                    hour = time.substring(0, indexAMPM);
                    type = time.substring(indexAMPM + 1);
                    if (type != "AM" && type != "PM") {
                        return false;
                    }
                } else {
                    hour = value.substring(indexTime + 1);
                }
            }
        }
    }
    dateFormat = formatForDate;
    if (value != "" && (isDate(date, dateFormat) == false || isValidHour(hour) == false)) {
        return false;
    }
    return true;
}

function isValidDateTime(value, format) {
    if (format == "yyyy. M. d a h:mm") {
        return auxDateTime(value, format, "yyyy. M. d");
    }
    if (format == "yyyy.MM.dd HH:mm" || format == "yyyy.M.d HH.mm") {
        return auxDateTime(value, format, "yyyy.MM.dd");
    }
    if (format == "MM-dd-yyyy hh:mm a") {
        return auxDateTime(value, format, "MM-dd-yyyy");
    }
    if (format == "yyyy/MM/dd hh:mm a" || format == "yyyy/MM/dd H:mm") {
        return auxDateTime(value, format, "yyyy/MM/dd");
    }
    if (format == "M/d/yyyy h:mm a") {
        return auxDateTime(value, format, "M/d/yyyy");
    }
    if (format == "dd-MM-yyyy HH:mm" || format == "dd-MM-yyyy hh:mm a" || format == "d-M-yyyy H:mm") {
        return auxDateTime(value, format, "dd-MM-yyyy");
    }
    if (format == "d.M.yyyy H:mm" || format == "dd.MM.yyyy HH:mm" || format == "d.MM.yyyy H:mm" || format == "dd.MM.yyyy H:mm" || format == "d.M.y H:mm") {
        return auxDateTime(value, format, "dd.MM.yyyy");
    }
    if (format == "yyyy-M-d H:mm" || format == "yyyy-MM-dd HH:mm" || format == "yyyy-M-d ah:mm") {
        return auxDateTime(value, format, "yyyy-M-d");
    }
    if (format == "dd/MM/yyyy hh:mm a" || format == "dd/MM/yyyy HH:mm" || format == "d/MM/yyyy hh:mm a" || format == "d/M/yyyy h:mm a" || format == "d/MM/yyyy HH:mm" || format == "HH:mm dd/MM/yyyy") {
        return auxDateTime(value, format, "dd/MM/yyyy");
    }
}

function isValidHour(strHour) {
    var index1 = strHour.indexOf(":");
    var hour = strHour.substring(0, index1);
    var minutes = strHour.substring(index1 + 1);
    if (isInteger(hour) && isInteger(minutes)) {
        if (hour > 12 || minutes > 59) {
            return false;
        } else {
            return true;
        }
    }
    return false;
}

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through strings characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}

function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31;
        if (i == 4 || i == 6 || i == 9 || i == 11) {
            this[i] = 30;
        }
        if (i == 2) {
            this[i] = 29;
        }
    }
    return this;
}

function isDate(dtStr, format) {
    var formatTemp = '';
    var dtCh = '';
    if (format == 'dd/MM/yyyy' || format == 'd/M/yyyy' || format == 'd/MM/yyyy') {
        formatTemp = "dd/mm/yyyy";
        var dtCh = '/';
    }
    if (format == 'dd.MM.yyyy' || format == 'd.M.yyyy' || format == 'd.MM.yyyy' || format == 'd.M.y') {
        formatTemp = "dd.mm.yyyy";
        var dtCh = '.';
    }
    if (format == 'dd-MM-yyyy' || format == 'd-M-yyyy') {
        formatTemp = "dd-MM-yyyy";
        var dtCh = '-';
    }
    if (format == 'yyyy/MM/dd' || format == 'yyyy/M/d') {
        formatTemp = "yyyy/MM/dd";
        var dtCh = '/';
    }
    if (format == 'yyyy-M-d' || format == 'yyyy-MM-dd') {
        formatTemp = "yyyy-M-d";
        var dtCh = '-';
    }
    if (format == 'M/d/yyyy' || format == 'MM-dd-yyyy') {
        formatTemp = "M/d/yyyy";
        var dtCh = '/';
    }
    if (format == 'yyyy.MM.dd' || format == 'yyyy.M.d') {
        formatTemp = "yyyy.MM.dd";
        var dtCh = '.';
    }
    if (format == 'yyyy.d.M') {
        formatTemp = "yyyy.d.M";
        var dtCh = '.';
    }
    if (format == 'yyyy.MM.dd.') {
        formatTemp = "yyyy.MM.dd.";
        var dtCh = '.';
    }
    if (format == 'yyyy. M. d') {
        formatTemp = "yyyy. M. d";
        var dtCh = '.';
    }
    return auxDate(dtStr, formatTemp, dtCh);
}

function auxDate(dtStr, format, dtCh) {
    var daysInMonth = DaysArray(12);
    var pos1 = dtStr.indexOf(dtCh);
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1);
    var strDay = '';
    var strMonth = '';
    var strYear = '';
    var tempStr = '';
    if (format == "yyyy. M. d") {
        strYear = dtStr.substring(0, pos1);
        var tempMonth = dtStr.substring(pos1 + 1, pos2);
        var tempDay = dtStr.substring(pos2 + 1);
        if (tempDay.charAt(0) == " ") {
            strDay = tempDay.substring(1, tempDay.length);
        }
        if (tempMonth.charAt(0) == " ") {
            strMonth = tempMonth.substring(1, tempMonth.length);
        }
        tempStr = strYear + '.' + strMonth + '.' + strDay;
    }
    if (format == "yyyy.MM.dd.") {
        strYear = dtStr.substring(0, pos1);
        strMonth = dtStr.substring(pos1 + 1, pos2);
        var tempDay = dtStr.substring(pos2 + 1);
        if (tempDay.charAt(tempDay.length - 1) == ".") {
            strDay = tempDay.substring(0, tempDay.length - 1);
            tempStr = dtStr.substring(0, dtStr.length - 1);
        }
    }
    if (format == "M/d/yyyy") {
        strMonth = dtStr.substring(0, pos1);
        strDay = dtStr.substring(pos1 + 1, pos2);
        strYear = dtStr.substring(pos2 + 1);
    }
    if (format == "yyyy.d.M") {
        strYear = dtStr.substring(0, pos1);
        strDay = dtStr.substring(pos1 + 1, pos2);
        strMonth = dtStr.substring(pos2 + 1);
    }
    if (format == "yyyy/MM/dd" || format == "yyyy-M-d" || format == "yyyy.MM.dd") {
        strYear = dtStr.substring(0, pos1);
        strMonth = dtStr.substring(pos1 + 1, pos2);
        strDay = dtStr.substring(pos2 + 1);
    }
    if (format == "dd/mm/yyyy" || format == "dd.mm.yyyy" || format == "dd-MM-yyyy") {
        strDay = dtStr.substring(0, pos1);
        strMonth = dtStr.substring(pos1 + 1, pos2);
        strYear = dtStr.substring(pos2 + 1);
    }
    strYr = strYear;
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1);
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1);
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1);
    }
    month = parseInt(strMonth);
    day = parseInt(strDay);
    year = parseInt(strYr);
    if (pos1 == -1 || pos2 == -1) {
        return false;
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        return false;
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        return false;
    }
    if (strYear.length != 4 || year == 0) {
        return false;
    }
    if (tempStr == "") {
        tempStr = dtStr;
    }
    if (tempStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(tempStr, dtCh)) == false) {
        return false;
    }
    return true;
}